//
//  ZMHomeTableViewController.swift
//  ZMWeiboSwift
//
//  Created by pg on 2017/7/22.
//  Copyright © 2017年 pg. All rights reserved.
//

import UIKit
import YYModel

class ZMHomeTableViewController: ZMBaseViewController {

   //引用viewModel
    fileprivate lazy var homeViewModel = ZMHomeViewModel()
    
    let text = UIRefreshControl()
    
    
    
    
    //懒加载下载活动指示器,,上啦刷新控件
    fileprivate lazy var pullUpView : UIActivityIndicatorView = {
       
        let indicator = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.whiteLarge)
        indicator.color = UIColor.orange
        return indicator
    }()
    
    //懒加载下拉刷新控件
    fileprivate lazy var pullDownView : ZMRefreshControl = {
    
         let downview = ZMRefreshControl()
        
        //为下拉刷新控件添加监听事件
        downview.addTarget(self, action: #selector(pullDownRefreshing), for: UIControlEvents.valueChanged)
        
         return downview
    }()
    //懒加载消息提示框控件
    fileprivate lazy var messageLabel : UILabel = {
    
        let msgLabel = UILabel()
        msgLabel.font = UIFont.systemFont(ofSize: 13)
        msgLabel.text = "没有加载到任何数据"
        msgLabel.textColor = UIColor.darkGray
        msgLabel.textAlignment = .center
        msgLabel.backgroundColor = UIColor.orange
        //默认隐藏
        msgLabel.isHidden = true
        return msgLabel
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        
        
        
        if !isLogin {
            //没有登录,就该访客视图信息
            visitorView?.updateVisitorInfo(message: "登录后,可以看到很多好友的动态哦", iconImageName:"visitordiscover_feed_image_house", isHomeView: true)
        }else
        {
            //登录,初始化tableview
            
            self.view.backgroundColor = UIColor.white
            
            //初始化tableview
            setupTabelView()
            setupMessgeLabel()
            loadData()
        }
        
    }
    //MARK:- 加载消息提示框
    func setupMessgeLabel() -> Void {
        
        if let nav = navigationController{
            
            //将消息提示框添加到导航控制器上面
            nav.view.insertSubview(messageLabel, belowSubview: nav.navigationBar)
            //设置messageLabel的frame
            messageLabel.frame = CGRect(x: 0, y: 64-35, width: UIScreen.main.bounds.width, height: 35)
        }
    }
    //MARK:- 显示消息框动画
    func startMessageLabelAnimation(message: String){
        
        //如果当前label正在显示  说明正在动画.这个时候就不需要在进行动画
        if messageLabel.isHidden == false{
            return
        }
        
        messageLabel.isHidden = false
        
        messageLabel.text = message
        
        UIView.animate(withDuration: 0.5, animations: { 
            self.messageLabel.transform = CGAffineTransform(translationX: 0, y: 35)
            
        }) { (isfinish) in
            
            UIView.animate(withDuration: 0.5, animations: { 
                self.messageLabel.transform = CGAffineTransform.identity
                
            }, completion: { (isfinish) in
                //动画之心结束,将messageLabel隐藏
                self.messageLabel.isHidden = true
            })
            
        }
        
        
        
    }

    func loadData() -> Void {
        
        homeViewModel.loadData(isUP: pullUpView.isAnimating) { (issuess,message) in
            
            
            self.endRefreshing()
            if issuess {
                self.tableView.reloadData()
            }
            
            //加载数据完毕,启动消息提示框动画
            self.startMessageLabelAnimation(message: message)
            
            
        }
    }
       
    func endRefreshing(){
        pullUpView.stopAnimating()
        pullDownView.endRefreshing()
    }
    
    //MARK:- tableview初始化
    func setupTabelView(){
        
        //注册cell
        tableView.register(ZMHomeTableViewCell.classForCoder(), forCellReuseIdentifier: "cell")
        //设置tableview高度
        tableView.estimatedRowHeight = 200
        
        tableView.separatorStyle = .none
     
        //上拉试图
        tableView.tableFooterView = pullUpView
       
        //下拉视图
        tableView.addSubview(pullDownView)
        
    }
    //MARK:- 下拉刷新事件
    func pullDownRefreshing(){
        
        //
        loadData()
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

  
}
//MARK:-tableview数据源方法
extension ZMHomeTableViewController{
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return homeViewModel.statusList.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! ZMHomeTableViewCell
        
        cell.statusViewModel = self.homeViewModel.statusList[indexPath.row]
        cell.selectionStyle = .none
        return cell
    }
    
    
    override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        //如果滚动到最后一个cell 就需要显示上啦view
        if indexPath.row == homeViewModel.statusList.count-1 && pullUpView.isAnimating == false {
            
            print("滚动到最后一个cell")
            pullUpView.startAnimating()
            loadData()
        }
    }
    
}
